-- UIDungeonClearNotify
-- create by zouyb
-- 地牢中通关提示

-- UIDungeonClearNotify继承自Layer
UIDungeonClearNotify = class("UIDungeonClearNotify", function()
    return cc.Layer:create();
end);

function UIDungeonClearNotify.create(title, gainSP)
    return UIDungeonClearNotify.new(title, gainSP);
end

local resize;

-- 构造函数
function UIDungeonClearNotify:ctor(title, gainSP)
    self:setName("UIDungeonClearNotify");

    local node = cc.CSLoader:createNode("layout/dungeon/DungeonClearNotify.csb");
    self:addChild(node);
    self.node = node;
    self.gainSp = gainSP;
    local kerning = 0;

    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -4;
    end

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 2);

    resize(node);

    local text = findChildByName(self.node, "CT/title");
    TextStyleM.setTextStyle(text, TextStyleM.TEXT_SIZE_LARGE3, TextStyleM.TEXT_COLOR_GOLD_YELLOW, true);
    text:setPositionX(-DESIGN_WIDTH);

    local spNode = findChildByName(self.node, "CT/sp_node");
    spNode:setPositionX(-DESIGN_WIDTH);

    local contentLabel = findChildByName(self.node, "CT/sp_node/content");
    TextStyleM.setTextStyle(contentLabel, TextStyleM.TEXT_SIZE_LARGE, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, kerning);

    local valueLabel = findChildByName(self.node, "CT/sp_node/value");
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_LARGE, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true);
    valueLabel:setString("1");

    -- 如果获得了探索点
    local content = "";
    if type(gainSP) == "number" and gainSP > 0 then
        content = getLocStr("gain_search_point2");
    end

    local function doEffect()
        self.isPlaying = true;
        self:playEffect(title, content, gainSP);
    end

    performWithDelay(self, doEffect, 0.01);

    -- 注册任意地方点击返回事件
    local bgImage = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if not self.canSkip then
                return;
            end

            if self.isPlaying then
                self:showItRightNow();
                return;
            end

            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIDungeonClearNotify");
        end
    end
    bgImage:addTouchEventListener(onBgBackClick);

    self.canSkip = false;

    local function setSkipFlag()
        self.canSkip = true;
    end

    -- 1秒后才可以点击关闭
    performWithDelay(self, setSkipFlag, 0.5);
end

-- 适配
resize = function(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

-- 直接显示结果
function UIDungeonClearNotify:showItRightNow()
    local bg = findChildByName(self.node, "bg/bgImg");
    bg:setOpacity(255);
    bg:stopAllActions();
    local delay = cc.DelayTime:create(2);
    local callFunc = cc.CallFunc:create(function()
        UIMgr.getCurrentScene():removeFormByName("UIDungeonClearNotify");
    end);
    bg:runAction(cc.Sequence:create(delay, callFunc));

    self:stopAllActions();
    self.isPlaying = false;

    local text = findChildByName(self.node, "CT/title");
    text:setPosition(cc.p(0, text:getPositionY()));
    text:stopAllActions();

    local spNode = findChildByName(self.node, "CT/sp_node");
    spNode:setPosition(cc.p(0, spNode:getPositionY()));
    spNode:stopAllActions();

    local contentLabel = findChildByName(self.node, "CT/sp_node/content");
    contentLabel:stopAllActions();
    if tostring(self.gainSp) ~= "" then
        contentLabel:setString(getLocStr("gain_search_point2"));
    end

    local valueLabel = findChildByName(self.node, "CT/sp_node/value");
    valueLabel:stopAllActions();
    valueLabel:setString(tostring(self.gainSp));
end

-- 显示通关后完美击杀
function UIDungeonClearNotify:playClearEffect(title, content, value)
    local text = findChildByName(self.node, "CT/title");
    text:setString(title);

    local parentNode = text:getParent();

    -- 针对其他语言版本的时间修正参数
    local fixCo = 1;
        if not isFullWidthLang() then
        fixCo = 0.5;
    end

    local titleFlyIn = cc.CallFunc:create(function()
        AudioM.playFx("perfect_kill");
        -- 标题飞入
        setTextRotate(string.getWCharCount(title), text, parentNode, 360, 1 / fixCo);
    end);

    local moveTo = cc.MoveTo:create(0.3 * fixCo, cc.p(0, text:getPositionY()));
    local delay1 = cc.DelayTime:create(0.4 * fixCo);
    local contentFlyIn = cc.CallFunc:create(function()
        playEffect(text, 1135, 20, 180);

        -- 内容显示
        if content ~= nil and content ~= "" then
            local spNode = findChildByName(self.node, "CT/sp_node");

            local icon       = findChildByName(spNode, "icon");
            local contentLabel = findChildByName(spNode, "content");
            contentLabel:setString(content);
            local valueLabel = findChildByName(spNode, "value");

            -- 获取文本框的长度，根据长度做偏移
            local labelWidth = contentLabel:getVirtualRenderer():getContentSize().width;
            local offsetX    = (labelWidth - 176) / 2;

            icon:setPositionX(icon:getPositionX() - offsetX);
            contentLabel:setPositionX(contentLabel:getPositionX() - offsetX);
            valueLabel:setPositionX(valueLabel:getPositionX() + offsetX);

            local delay1 = cc.DelayTime:create(0.3 * fixCo);
            local moveTo2 = cc.MoveTo:create(0.1 * fixCo, cc.p(0, spNode:getPositionY()));
            local delay2 = cc.DelayTime:create(0.1 * fixCo);
            local callFunc2 = cc.CallFunc:create(function()
                scoreScrolling(valueLabel, value, nil, 150);
            end);

            spNode:runAction(cc.Sequence:create(delay1, moveTo2, delay2, callFunc2));
        end
    end);

    text:runAction(cc.Sequence:create(titleFlyIn, moveTo, delay1, contentFlyIn));
end

-- 播放效果
function UIDungeonClearNotify:playEffect(title, content, value)
    self:playClearEffect(title, content, value);

    -- bg渐现
    local bg = findChildByName(self.node, "bg/bgImg");
    local fadeIn = cc.FadeIn:create(0.3);
    local delay = cc.DelayTime:create(3.5);
    local fadeOut = cc.FadeOut:create(0.3);
    local callFunc = cc.CallFunc:create(function()
        UIMgr.getCurrentScene():removeFormByName("UIDungeonClearNotify");
    end);
    bg:runAction(cc.Sequence:create(fadeIn, delay, fadeOut, callFunc));

end